package com.itlong.cloud.sdk.service.impl;

import com.itlong.cloud.POJO.DTO.sdk.CloudDeviceSDKDTO;
import com.itlong.cloud.POJO.DTO.sdk.sendPush.SendPushMessageDTO;
import com.itlong.cloud.POJO.VO.sdk.SDKEquipRecordGetVO;
import com.itlong.cloud.POJO.VO.sdk.SDKProjectBuildInfoVO;
import com.itlong.cloud.POJO.VO.sdk.SDKProjectRoomInfoVO;
import com.itlong.cloud.POJO.VO.sdk.SDKProjectUnitInfoVO;
import com.itlong.cloud.enumerate.PropertyEquipTypeEnum;
import com.itlong.cloud.sdk.dao.ISDKServerProjectDao;
import com.itlong.cloud.sdk.service.ISDKServerProjectService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.network.https.HttpsPost;
import com.itlong.cloud.utils.page.Page;
import org.apache.ibatis.annotations.Case;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 * SDK项目服务层接口实现类
 * </desc>
 *
 * @createDate 2019/11/8
 */
@Service
public class SDKServerProjectServiceImpl implements ISDKServerProjectService {

    private static Logger LOG = LoggerFactory.getLogger(SDKServerProjectServiceImpl.class);

    @Autowired
    private ISDKServerProjectDao isdkServerProjectDao;

    /**
     * <desc>
     *      获取项目的楼栋信息列表
     * </desc>
     *
     * @param dto 查询参数
     * @return 楼栋信息列表
     * @author Juguang.S
     * @createDate 2019/11/7
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<SDKProjectBuildInfoVO> getBuildList(CloudDeviceSDKDTO dto) throws Exception {

        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(dto);
        params.put("projectId",dto.getProjectId());

        Page<SDKProjectBuildInfoVO> page = new Page<>();
        page.getPage().setPageSize(dto.getPageSize());
        page.getPage().setCurrentPage(dto.getCurrentPage());
        params.put("startSlip",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        //填充返回数据集
        List<SDKProjectBuildInfoVO> sdkProjectBuildInfoVOS = isdkServerProjectDao.getBuildListByPage(params);
        page.setRows(sdkProjectBuildInfoVOS, isdkServerProjectDao.getBuildInfoCount(params));
        return page;
    }

    /**
     * <desc>
     *      获取项目楼栋的单元信息列表
     * </desc>
     *
     * @param dto 查询参数
     * @return 楼栋信息列表
     * @author Juguang.S
     * @createDate 2019/11/7
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<SDKProjectUnitInfoVO> getUnitList(CloudDeviceSDKDTO dto) throws Exception {

        Map<String, Object> params = SqlUtil.convertBeanToMapParams(dto);
        params.put("projectId",dto.getProjectId());

        Page<SDKProjectUnitInfoVO> page = new Page<>();
        page.getPage().setPageSize(dto.getPageSize());
        page.getPage().setCurrentPage(dto.getCurrentPage());
        params.put("startSlip",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        //填充返回数据集
        List<SDKProjectUnitInfoVO> propertyProjectUnitInfoVOS = isdkServerProjectDao.getBuildUnitListByPage(params);
        page.setRows(propertyProjectUnitInfoVOS, isdkServerProjectDao.getBuildUnitInfoCount(params));
        return page;
    }

    /**
     * <desc>
     *     获取项目房间信息列表
     * </desc>
     *
     * @param dto 查询参数
     * @return 楼栋信息列表
     * @author Juguang.S
     * @createDate 2019/11/8
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<SDKProjectRoomInfoVO> getRoomList(CloudDeviceSDKDTO dto) throws Exception {

        Map<String, Object> params = SqlUtil.convertBeanToMapParams(dto);
        params.put("projectId",dto.getProjectId());

        Page<SDKProjectRoomInfoVO> page = new Page<>();
        page.getPage().setPageSize(dto.getPageSize());
        page.getPage().setCurrentPage(dto.getCurrentPage());
        params.put("startSlip",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        //填充返回数据集
        List<SDKProjectRoomInfoVO> propertyProjectUnitInfoVOS = isdkServerProjectDao.getRoomListByPage(params);
        page.setRows(propertyProjectUnitInfoVOS, isdkServerProjectDao.getRoomInfoCount(params));
        return page;
    }

    /**
     * <desc>
     *     获取项目的通行记录列表
     * </desc>
     *
     * @param dto 查询参数
     * @return 通行记录信息列表
     * @author Juguang.S
     * @createDate 2019/11/11
     */
    @Override
    public Page<SDKEquipRecordGetVO> getPassRecordByPage(CloudDeviceSDKDTO dto) throws Exception {
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(dto);
        params.put("projectId", dto.getProjectId());

        Page<SDKEquipRecordGetVO> page = new Page<>();
        page.getPage().setPageSize(dto.getPageSize());
        page.getPage().setCurrentPage(dto.getCurrentPage());
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());

        List<SDKEquipRecordGetVO> recordList = new ArrayList<>();
        Integer recordCount = 0;
        //根据不同的设备型号查询通行记录
        Integer deviceType = Integer.parseInt(dto.getRecordType());
        //如果请求类型为访客
        if(dto.getUserType().equals("2")){
            List<String> typeList = new ArrayList<>();
            if(dto.getRecordType().equals("1")){
                typeList.add("12");
            }
            if(dto.getRecordType().equals("2")){
                typeList.add("22");
                typeList.add("23");
            }
            if(dto.getRecordType().equals("3")){
                typeList.add("3");
            }
            params.put("recordType",typeList);
            long startTime = System.currentTimeMillis();
            recordList = isdkServerProjectDao.getVisitorPassRecordByPage(params);
            recordCount = isdkServerProjectDao.getVisitorPassRecordCount(params);
//            long endTime = System.currentTimeMillis();
//            long timeConsuming = endTime - startTime;
//            LOG.info("【请求人脸识别访客通行记录数据耗时】：" + timeConsuming + "ms,"+"【传参】：deviceType："+dto.getDeviceType()+",projectId:"+dto.getProjectId()+
//                    ",recordType:"+dto.getRecordType()+",startTime:"+dto.getStartTime()+",endTime:"+dto.getEndTime()+",userType:"+dto.getUserType()+
//                    ",currentPage:"+dto.getCurrentPage()+",pageSize:"+dto.getPageSize());
        }
        //请求类型为业主
        if(dto.getUserType().equals("1")) {
            switch (PropertyEquipTypeEnum.getByEquipType(deviceType)) {
                case ELEVATOR:
                    long startTime = System.currentTimeMillis();
                    recordList = isdkServerProjectDao.getElevatorRecordByPage(params);
                    recordCount = isdkServerProjectDao.getElevatorRecordCount(params);
//                    long endTime = System.currentTimeMillis();
//                    long timeConsuming = endTime - startTime;
//                    LOG.info("【请求人脸识别业主电梯通行记录数据耗时】：" + timeConsuming + "ms," + "【传参】：deviceType：" + dto.getDeviceType() + ",projectId:" + dto.getProjectId() +
//                            ",recordType:" + dto.getRecordType() + ",startTime:" + dto.getStartTime() + ",endTime:" + dto.getEndTime() + ",userType:" + dto.getUserType() +
//                            ",currentPage:" + dto.getCurrentPage() + ",pageSize:" + dto.getPageSize());
                    break;
                case ENTRANCE:
                    long startTimeEntrance = System.currentTimeMillis();
                    recordList = isdkServerProjectDao.getEntranceRecordByPage(params);
                    recordCount = isdkServerProjectDao.getEntranceRecordCount(params);
//                    long endTimeEntrance = System.currentTimeMillis();
//                    long timeConsumingEntrance = endTimeEntrance - startTimeEntrance;
//                    LOG.info("【请求人脸识别业主门禁通行记录数据耗时】：" + timeConsumingEntrance + "ms," + "【传参】：deviceType：" + dto.getDeviceType() + ",projectId:" + dto.getProjectId() +
//                            ",recordType:" + dto.getRecordType() + ",startTime:" + dto.getStartTime() + ",endTime:" + dto.getEndTime() + ",userType:" + dto.getUserType() +
//                            ",currentPage:" + dto.getCurrentPage() + ",pageSize:" + dto.getPageSize());
                    break;
                case CLOUD_INTERCOM:
                    long startTimeTalk = System.currentTimeMillis();
                    recordList = isdkServerProjectDao.getCloudIntercomRecordByPage(params);
                    recordCount = isdkServerProjectDao.getCloudIntercomRecordCount(params);
//                    long endTimeTalk = System.currentTimeMillis();
//                    long timeConsumingTalk = endTimeTalk - startTimeTalk;
//                    LOG.info("【请求人脸识别业主云对讲通行记录数据耗时】：" + timeConsumingTalk + "ms," + "【传参】：deviceType：" + dto.getDeviceType() + ",projectId:" + dto.getProjectId() +
//                            ",recordType:" + dto.getRecordType() + ",startTime:" + dto.getStartTime() + ",endTime:" + dto.getEndTime() + ",userType:" + dto.getUserType() +
//                            ",currentPage:" + dto.getCurrentPage() + ",pageSize:" + dto.getPageSize());
                    break;
            }
        }
        page.setRows(recordList, recordCount);
        return page;
    }

    /**
     * <desc>
     *     删除时间段通行记录
     * </desc>
     *
     * @param dto 查询参数
     * @return 通行记录信息列表
     * @author Qiang.S
     * @createDate 2020/07/02
     */
    @Override
    public Integer deleteRecordByDate(CloudDeviceSDKDTO dto) throws Exception {
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(dto);
        params.put("projectId", dto.getProjectId());

        //recordType通行记录类型 1电梯 2门禁 3云对讲
        //userType 用户类型 1业主 2访客
        Integer recordCount = 0;
        Integer deviceType = Integer.parseInt(dto.getRecordType());
        //根据不同的设备型号查询通行记录
        //如果请求类型为访客
        if (dto.getUserType().equals("2")) {
            recordCount = isdkServerProjectDao.deleteVisitorRecordByDate(params);
        }
        //请求类型为业主
        if (dto.getUserType().equals("1")) {
            switch (PropertyEquipTypeEnum.getByEquipType(deviceType)) {
                case ELEVATOR:
                    recordCount = isdkServerProjectDao.deleteElevatorRecordByDate(params);
                    break;
                case ENTRANCE:
                    recordCount = isdkServerProjectDao.deleteEntranceRecordByDate(params);
                    break;
                case CLOUD_INTERCOM:
                    recordCount = isdkServerProjectDao.deleteIntercomRecordByDate(params);
                    break;
            }
        }
        return recordCount;
    }

    /**
     * <desc>
     * 查询第三方消息回调地址
     * </desc>
     *
     * @Author chengshican
     * @Date 2020-11-23 11:08
     * @Param
     * @Return
     * @Exception
     */
    @Override
    public Integer getSendMessgeUrl(String type,double newVersion) throws Exception  {
        List<SendPushMessageDTO> sendMessgeUrl = isdkServerProjectDao.getSendMessgeUrl();
        switch (type){

            //云对讲
            case   "0"  :
                for (SendPushMessageDTO dto : sendMessgeUrl) {
                    HttpsPost.sendGet(dto.getUpdateMessageCallbackUrl(),"尊敬的用户，您好！云对讲api的版本已经更新，您当前版本为"+dto.getCloudIntercomSdkVersion()+"，是否升级到最新版本"+newVersion+"?");
                }
                break;

            //蓝牙SDK
            case   "1"  :
                for (SendPushMessageDTO dto : sendMessgeUrl) {
                    HttpsPost.sendGet(dto.getUpdateMessageCallbackUrl(),"尊敬的用户，您好！蓝牙SDK的版本已经更新，您当前版本为"+dto.getBluetoothSdkVersion()+"，是否升级到最新版本"+newVersion+"?");
                }
                break;

            //二维码SDK
            case   "2"  :
                for (SendPushMessageDTO dto : sendMessgeUrl) {
                    HttpsPost.sendGet(dto.getUpdateMessageCallbackUrl(),"尊敬的用户，您好！二维码SDK的版本已经更新，您当前版本为"+dto.getQrCodeSdkVersion()+"，是否升级到最新版本"+newVersion+"?");
                }
                break;

            //云电梯API
            case   "3"  :
                for (SendPushMessageDTO dto : sendMessgeUrl) {
                    HttpsPost.sendGet(dto.getUpdateMessageCallbackUrl(),"尊敬的用户，您好！云电梯API的版本已经更新，您当前版本为"+dto.getCloudElevatorApiVersion()+"，是否升级到最新版本"+newVersion+"?");
                }
                break;

            //云门禁API
            case   "4"  :
                for (SendPushMessageDTO dto : sendMessgeUrl) {
                    HttpsPost.sendGet(dto.getUpdateMessageCallbackUrl(),"尊敬的用户，您好！云门禁API版本已经更新，您当前版本为"+dto.getCloudEntranceApiVersion()+"，是否升级到最新版本"+newVersion+"?");
                }
                break;

            //人脸识别API
            case   "5"  :
                for (SendPushMessageDTO dto : sendMessgeUrl) {
                    HttpsPost.sendGet(dto.getUpdateMessageCallbackUrl(),"尊敬的用户，您好！人脸识别API的版本已经更新，您当前版本为"+dto.getFaceRecognitionApiVersion()+"，是否升级到最新版本"+newVersion+"?");
                }
                break;
            //机器人无感通行
            case   "6"  :
                for (SendPushMessageDTO dto : sendMessgeUrl) {
                    HttpsPost.sendGet(dto.getUpdateMessageCallbackUrl(),"尊敬的用户，您好！机器人无感通行版本已经更新，您当前版本为"+dto.getRobotNoninductiveVersion()+"，是否升级到最新版本"+newVersion+"?");
                }
                break;

            //机器人无感通行机器人无感通行机器人无感通行
            case   "7"  :
                for (SendPushMessageDTO dto : sendMessgeUrl) {
                    HttpsPost.sendGet(dto.getUpdateMessageCallbackUrl(),"尊敬的用户，您好！机器人单梯云的版本已经更新，您当前版本为"+dto.getRobotToCloudVersion()+"，是否升级到最新版本"+newVersion+"?");
                }
                break;
            //机器人云门禁
            case   "8"  :
                for (SendPushMessageDTO dto : sendMessgeUrl) {
                    HttpsPost.sendGet(dto.getUpdateMessageCallbackUrl(),"尊敬的用户，您好！机器人云门禁的版本已经更新，您当前版本为"+dto.getRobotEntranceVersion()+"，是否升级到最新版本"+newVersion+"?");
                }
                break;

            //离线二维码SDK
            case   "9"  :
                for (SendPushMessageDTO dto : sendMessgeUrl) {
                    HttpsPost.sendGet(dto.getUpdateMessageCallbackUrl(),"尊敬的用户，您好！离线二维码SDK的版本已经更新，您当前版本为"+dto.getOfflineQrCodeSdkVersion()+"，是否升级到最新版本"+newVersion+"?");
                }
                break;

            //微信小插件二维码
            case   "10"  :
                for (SendPushMessageDTO dto : sendMessgeUrl) {
                    HttpsPost.sendGet(dto.getUpdateMessageCallbackUrl(),"尊敬的用户，您好！微信小插件二维码的版本已经更新，您当前版本为"+dto.getWechatSmallQrCodeVersion()+"，是否升级到最新版本"+newVersion+"?");
                }
                break;

            //微信小插件蓝牙
            case   "11"  :
                for (SendPushMessageDTO dto : sendMessgeUrl) {
                    HttpsPost.sendGet(dto.getUpdateMessageCallbackUrl(),"尊敬的用户，您好！微信小插件蓝牙的版本已经更新，您当前版本为"+dto.getWechatSmallBluetoothDockingVersion()+"，是否升级到最新版本"+newVersion+"?");
                }
                break;

            //在线二维码API
            case   "12"  :
                for (SendPushMessageDTO dto : sendMessgeUrl) {
                    HttpsPost.sendGet(dto.getUpdateMessageCallbackUrl(),"尊敬的用户，您好！在线二维码API的版本已经更新，您当前版本为"+dto.getOnlineQrCodeApiVersion()+"，是否升级到最新版本"+newVersion+"?");
                }
                break;

        }








        return sendMessgeUrl.size();
    }

    /**
     * <desc>
     * 修改第三方对接接口的版本号
     * </desc>
     *
     * @param dto
     * @Author chengshican
     * @Date 2020-11-23 11:09
     * @Param SendPushMessageDTO
     * @Return
     * @Exception
     */
    @Override
    public Integer updateSDKApiVersion(SendPushMessageDTO dto) throws Exception  {
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(dto);
        return isdkServerProjectDao.updateSDKApiVersion(params);
    }
}
